<?php

defined( 'IN_DICE' ) or die( 'Access Error!' );

class ModelMember extends base {
  
  function __construct() {
    parent::__construct();
  }
  
  /**
   * 根据用户ID来获得用户的游戏信息
   *
   * @param int $uid 用户ID
   * @param bool $cache 是否在缓存中
   * @return array
   */
  public function get( $uid, $cache = true ) {
    if( !$uid = uint( $uid ) ) {
      return array();
    }
    $sql = "SELECT * FROM $this->usergame WHERE uid='$uid' LIMIT 1";
    $aUser = model::db()->get_one( $sql );
    return $aUser;
  }

  /**
   * 根据uid来批量获取用户的个人资料
   * @param array $aUids 要查询的用户ID
   * @return array
   */
  public function getMulti( $aUids ) {
    return $aR;
  }
  
  /**
   * 根据用户的uid来获取用户的头像昵称等信息
   *
   * @param int $uid
   * @return array
   */
  public function getpbyuid( $uid, $cache = true ) {
    if( !$uid = uint( $uid ) ) {
      return array();
    }
    $sql = "SELECT * FROM $this->userinfo WHERE uid='$uid' LIMIT 1";
    $aP = model::db()->get_one( $sql );
    return $aP;
  }
  
  /**
   * 根据用户的站点ID和平台ID来获取用户的头像昵称等信息
   *
   * @param string $puid 
   * @param int $pfid
   * @param bool $cache
   * @return array
   */
  public function getp( $puid, $pfid = 1, $cache = true ) {
    if( !$puid ) {
      return array();
    }
    $puid = model::db()->escape( $puid );
    $uid = model::member()->getuid($puid, $pfid);
    return model::member()->getpbyuid( $uid, $cache );
  }

  /**
   * 根据 puid, pfid 来获取用户的 uid
   * @param string $puid
   * @param int $pfid
   * @return 用户的uid
   */
  public function getuid($puid, $pfid) {
    if( !$puid || (!$pfid = uint( $pfid ) ) ) {
      return 0;
    }
    $puid = model::db()->escape( $puid );
    $sql = "SELECT uid FROM $this->userinfo WHERE puid='$puid' AND pfid='$pfid' LIMIT 1";
    $aT = model::db()->get_one($sql);
    if (isset($aT['uid']) && $aT['uid'] > 0) {
      $uid = $aT['uid'];
    }
    return (int)$uid;
  }

  /**
   * 批量根据 puid, pfid 来获取用户的 uid
   * @param string $puid
   * @param int $pfid
   * @return array 用户的uid 结构 array( puid => uid, ... )
   */
  public function getuidMulti($aPuid, $pfid) {
  }
  
  /**
   * 注册一个用户
   *
   * @param array $aU
   * @return int
   */
  public function insert( $aU ) {
    if( ( count( $aU ) < 3 ) || ( !strlen( $aU['puid'] ) ) ) {
      return 0;
    }
    
    $aM['puid'] = model::db()->escape( $aU['puid'] );
    $aM['pfid'] = uint( $aU['pfid'] );
    $aM['usid'] = uint( $aU['usid'] );
    $aM['usex'] = uint( $aU['usex'] );
    $aM['uname'] = model::db()->escape( $aU['uname'] );
    $aM['uface'] = model::db()->escape( $aU['uface'] );
    $aM['utime'] = time();
    
    $sql  = "INSERT INTO $this->userinfo SET puid='{$aU['puid']}',
            pfid='{$aM['pfid']}',usid='{$aM['usid']}',usex='{$aM['usex']}',
            uname='{$aM['uname']}',uface='{$aM['uface']}',
            utime='{$aM['utime']}'"; //插入用户的平台信息
    model::db()->exe( $sql );
    $uid = model::db()->insertid();

    if( $uid ) { //平台方插入成功,则来插入游戏信息表
      $aM['uid'] = $uid;
      $aM['uchip'] = (int)all::$cfg['firstin'];
      $aM['ugtime'] = $aM['utime'];
      $sql = "INSERT IGNORE INTO $this->usergame SET uid='$uid',uchip='{$aM['uchip']}',ugtime='{$aM['utime']}' ";
      model::db()->exe( $sql );
    }
    
    return $uid;
  }

  /**
   * 修改用戶的平台信息,如個人头像昵称等
   * @param int $uid 用戶ID
   * @param array $aU 要修改的數據
   * @return int
   */
  public function update( $uid, $aU ) {
    if( (!$uid = uint( $uid ) ) || empty( $aU ) ) {
      return 0;
    }
    isset( $aU['usid'] ) && $aM['usid'] = uint( $aU['usid'] );
    isset( $aU['usex'] ) && $aM['usex'] = uint( $aU['usex'] );
    isset( $aU['uname'] ) && $aM['uname'] = model::db()->escape( $aU['uname'] );
    isset( $aU['uface'] ) && $aM['uface'] = model::db()->escape( $aU['uface'] );
    $set = '';
    $flag = 0;
    if( $aM ) {
      foreach( $aM as $k => $v ) {
        $v = model::db()->escape( $v );
        $set .= "$k='$v',";
      }
      $set = substr( $set, 0, -1 );
      $sql = "UPDATE $this->userinfo SET $set WHERE uid='$uid' LIMIT 1";
      model::db()->exe($sql);
      $flag = model::db()->update_rows();
      $flag && model::mredis('user')->hMSet(k::u($uid), $aM); //更新redis中的用户信息
    }
    return $flag;
  }
  
  /**
   * 用户的酒量升级
   *
   * @param int $uid
   * @param int $wlevel
   * @return int
   */
  public function setwinelevel( $uid, $wlevel ) {
    if( ( !$uid = uint( $uid ) ) || ( !$wlevel = uint( $wlevel ) ) ) {
      return 0;
    }
    
    $sql = "UPDATE $this->usergame SET winelevel='$wlevel' WHERE uid='$uid' LIMIT 1";
    model::db()->exe( $sql );
    $flag = model::db()->update_rows();
    return (int)$flag;
  }
  
  /**
   * 用户的酒量升级
   *
   * @param int $uid
   * @param int $wlevel
   * @return int
   */
  public function setlevel( $uid, $ulevel ) {
    if( ( !$uid = uint( $uid ) ) || ( !$ulevel = uint( $ulevel ) ) ) {
      return 0;
    }
    
    $sql = "UPDATE $this->usergame SET level='$ulevel' WHERE uid='$uid' LIMIT 1";
    model::db()->exe( $sql );
    $flag = model::db()->update_rows();
    return (int)$flag;
  }
  
  /**
   * 设置用户钱数
   *
   * @param int $uid
   * @param int $uchip
   * @param int $flag 0加1减
   * @return int
   */
  public function setchip( $uid, $uchip, $flag = 0 ) {
    if( ( !$uid = uint( $uid ) ) || ( !$uchip = uint( $uchip ) ) ) {
      return 0;
    }
    
    $flag = uint( $flag );
    $uchip = $flag == 1 ? -$uchip : $uchip;
    $sql = "UPDATE $this->usergame SET uchip=uchip+'$uchip' WHERE uid='$uid' " .
            ($uchip>0 ? '' : "AND uchip+'$uchip'>=0") . " LIMIT 1";
    model::db()->exe( $sql );
    $res = model::db()->update_rows();
    
    return (int)$res;
  }
  
  /**
   * 加判断的更新用户的最后登陆时间为现在
   * @param int $uid 用户ID
   * @param bool $iscday 是否连续登陆
   * @return int
   */
  public function uplogintime( $uid, $iscday = false ) {
    if( !$uid = uint( $uid ) ) {
      return 0;
    }
    $ugtime = time();
    $today = strtotime( 'today' );
    $upcday = $iscday === true ? ",uldays=uldays+1" : ',uldays=1';
    $sql = "UPDATE $this->usergame SET ugtime='$ugtime' $upcday
            WHERE uid='$uid' AND ugtime<'$today' LIMIT 1";
    model::db()->exe( $sql );
    $flag = model::db()->update_rows();
    
    return $flag;
  }
  
  /**
   * 根据用户昵称获得用户资料,仅限测试环境下
   *
   * @param string $unick
   * @return array
   */
  public function getonebyunick( $unick ) {
    if( !strlen( $unick ) ) {
      return array();
    }
    if( PRODUCT_ENV === true ) {
      return array();
    }
    $unick = model::db()->escape( $unick );
    $sql = "SELECT * FROM $this->userinfo WHERE uname='$unick' LIMIT 1";
    $aP = model::db()->get_one( $sql );
    
    return $aP;
  }
  
  /**
   * 按顺序返回前100个用户供测试
   *
   * @return array
   */
  public function get100() {
    $sql = "SELECT * FROM $this->userinfo LIMIT 100";
    $aP = model::db()->get_all( $sql );
    
    return $aP;
  }

  /**
   * 根据输入的变量来获得多条用户信息
   * @param array $params 变量
   * @return array
   */
  public function finduser( $params ) {
    $aR = array();
    if( !is_array( $params ) ) {
      return $aR;
    }
    foreach( $params as $k => $v ) {
      $params[$k] = model::db()->escape( $v );
    }
    if( isset( $params['uid'] ) && ( $uid = uint( $params['uid'] ) ) ) {
      $aOne = model::member()->getpbyuid($uid);
      $aR = is_array( $aOne ) ? array( $aOne ) : array();
    } else if( isset( $params['puid'] ) ) {
      $puid = $params['puid'];
      $sql = "SELECT * FROM $this->userinfo WHERE puid='$puid' ";
      $aR = model::db()->get_all($sql);
    } else if ( isset( $params['uname'] ) ) { //控制用用户名搜索只返回最多10条结果
      $uname = $params['uname'];
      $sql = "SELECT * FROM $this->userinfo WHERE uname='$uname' LIMIT 10";
      $aR = model::db()->get_all($sql);
    }
    foreach( $aR as $k => $v ) {
      $aR[$k]['homepage'] = model::member()->getHomePage( $v['puid'], $v['pfid'] );
    }

    return $aR;
  }

  /**
   * 根据用户的puid和平台id来获得用户的个人主页
   *
   * @param string $puid
   * @param int $pfid
   * @return string
   */
  public function getHomePage( $puid, $pfid ) {
    $homepage = '';
    switch ( $pfid ) {
      case 1:
        $homepage = "http://www.renren.com/profile.do?id={$puid}";
        break;
      case 2:
        $homepage = "http://uchome.manyou.com/profile/{$puid}";
        break;
      case 3:
        $homepage = 'http://home.51.com/' . $puid;
        break;
      case 4:
        $homepage = "http://www.facebook.com/profile.php?id={$puid}";
        break;
      case 5:
        $homepage = "http://t.sdo.com/home/{$puid}";
        break;
      default:
        $homepage = '';
        break;
    }

    return $homepage;
  }

}













